<template>
  <div class="app-container">
    <!-- 添加或修改合同类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body v-drag-fullscreen>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="8" :xs="24">
            <el-form-item label="所属小区" prop="communityId">
              <community-filterable v-model="form.communityId" />
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="类型名称" prop="typeName">
              <el-input v-model="form.typeName" placeholder="请输入类型名称" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="类型代码" prop="typeCode">
              <el-input v-model="form.typeCode" placeholder="请输入类型代码" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="24" :xs="24">
            <el-form-item label="收费标准" prop="chargeItemIds">
              <LSelect
                v-model="form.chargeItemIds"
                url="/payfee/norm/listIn"
                optionValue="normId"
                optionLabel="normNames"
                multiple
                :queryParams="{deptId: form.communityId||-1}"
                @cv="changeChargeItem"
                ref="chargeItemRefs"
                placeholder="请选择关联收费标准(请先选择所属小区)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24" :xs="24">
            <first-party-list ref="partyListRef" :contractCategoryId="form.id" />
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {getContractCategory, addContractCategory, updateContractCategory} from "@/api/lease/ContractCategory";
  import FirstPartyList from "./firstPartyList"
  export default {
    name: "ContractCategoryFrom",
    components:{ FirstPartyList },
    data() {
      return {
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //是否禁用
        disabled: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          communityId: [
            {required: true, message: "所属小区不能为空", trigger: "blur"}
          ],
          typeName: [
            {required: true, message: "类型名称不能为空", trigger: "blur"}
          ],
          typeCode: [
            {required: true, message: "类型代码不能为空", trigger: "blur"}
          ],
          chargeItemIds: [
            {required: true, message: "收费标准不能为空", trigger: "blur"}
          ],
        },
      };
    },
    created() {

    },
    methods: {
      show(option) {
        this.reset();
        this.disabled = option.type == 'VIEW'
        if (option.type == 'ADD') {
          this.open = true;
          this.title = "新增合同类型";
        } else if (option.type == 'UPDATE' || option.type == 'VIEW') {
          getContractCategory(option.id).then(response => {
            this.form = response.data;

            if (option.type == 'VIEW') {
              this.title = "查看合同类型";
            } else {
              this.title = "修改合同类型";
            }
            this.open = true;
          });
        }
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
        this.$emit("cancel")
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          communityId: null,
          typeName: null,
          typeCode: null,
          chargeItemIds: null,
          chargeItemNames: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          firsPartyList:[]
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let msg = []
            let firsPartyList = this.$refs.partyListRef.getFirsPartyList()
            for(let i in firsPartyList){
              let rowIndex = parseInt(i)+1
              let row = firsPartyList[i]
              if(!row.partyName){
                msg.push(`第${rowIndex}行甲方名称不能为空`)
              }
              if(!row.partyPhone){
                msg.push(`第${rowIndex}行甲方手机号不能为空`)
              }
              if(!row.partyContacts){
                msg.push(`第${rowIndex}行甲方联系人不能为空`)
              }
            }
            if(msg.length > 0){
              this.msgError(msg.join(','))
              return
            }
            this.form.firsPartyList = firsPartyList
            if (this.form.id != null) {
              updateContractCategory(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.cancel()
              });
            } else {
              addContractCategory(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.cancel()
              });
            }
          }
        });
      },
      changeChargeItem(v){
        this.form.chargeItemNames = this.$refs.chargeItemRefs.selectDictLabels();
      }
    }
  };
</script>
